package com.example.combwyaq.mvp.model;

import android.util.Log;

import com.example.combwyaq.http.Api;
import com.example.combwyaq.http.HttpFactory;
import com.example.combwyaq.http.HttpType;
import com.example.combwyaq.mvp.ChangeFuncation;
import com.example.combwyaq.mvp.contract.LREContract;
import com.example.combwyaq.mvp.model.entity.BaseEntity;
import com.example.combwyaq.mvp.model.entity.ChannelEntity;
import com.example.combwyaq.mvp.model.entity.ImageBannerEntity;
import com.example.combwyaq.mvp.model.entity.NewsEntity;
import com.example.combwyaq.mvp.model.entity.TextBannerEntity;
import com.example.combwyaq.visualinterface.live.entity.LiveEntity;
import com.example.combwyaq.visualinterface.people.entity.UserFollowEntity;
import com.example.combwyaq.visualinterface.people.entity.FollowEntity;
import com.example.combwyaq.visualinterface.people.entity.RecommendEntity;
import com.example.combwyaq.visualinterface.video.entity.VideoNewsEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;

/**
 * 针对列表接口界面的请求model
 * */
public class LREModel extends BaseModel implements LREContract.LREModel {
    private static final String TAG = "LREModel";

    @Inject
    public LREModel() {
    }

    @Override
    public Observable<BaseEntity> requestAll(Map<String, Object>... maps) {

        /*根据传递进来的maps数量创建生成专门用来存放被观察着的集合*/
        List<Observable<BaseEntity>>observables=new ArrayList<>();
        for (int i = 0; i < maps.length; i++) {
            Map<String,Object>map=maps[i];
            observables.add(change2BaseEntity(map));
        }
        return Observable.merge(observables);
    }
/*    异步任务转化为统一的baseEntity任务*/
    private Observable<BaseEntity> change2BaseEntity(Map<String,Object>map){
        int code = (int) map.get("code");
        map.remove("code");
        Observable<BaseEntity> ob =null;
        switch (code){
            case HttpCode.USERCHANNELCODE:
                //获取首页用户频道接口
                ob =  HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit().create(Api.class)
                        .requestUserChannels(createBody(map))
                        .map(new ChangeFuncation<ChannelEntity>());
                break;
            case HttpCode.CHANNELNEWS:
                //按频道获取新闻列表接口
                ob =  HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit().create(Api.class)
                        .requestNews(createBody(map))
                        .map(new ChangeFuncation<NewsEntity>());
                break;
            case HttpCode.IMAGERBANNERCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKEN)
                        .getRetrofit().create(Api.class)
                        .requestImageBanner().map(new ChangeFuncation<ImageBannerEntity>());
                break;
            case HttpCode.TEXTBANNERCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKEN)
                        .getRetrofit().create(Api.class)
                        .requestTextBanner().map(new ChangeFuncation<TextBannerEntity>());
                break;
            case HttpCode.RECOMMENDNEWSCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKEN)
                        .getRetrofit().create(Api.class)
                        .requestRecommendNews()
                        .map(new ChangeFuncation<RecommendEntity>());
                break;
            case HttpCode.FOLLOWNEWSCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit().create(Api.class)
                        .requestFollowNews(createBody(map))
                        .map(new ChangeFuncation<FollowEntity>());
                break;
            case HttpCode.USERFOLLOWCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit().create(Api.class)
                        .requestUserFollow(createBody(map))
                        .map(new ChangeFuncation<UserFollowEntity>());
                break;
            case HttpCode.VIDEONEWSCODE:
                ob=HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit().create(Api.class)
                        .requestVideoNews(createBody(map)).map(
                                new ChangeFuncation<VideoNewsEntity>());
                break;
            case HttpCode.LIVECODE:
                ob=HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit().create(Api.class)
                        .requestLives(createBody(map))
                        .map(new ChangeFuncation<LiveEntity>());

        }
        Log.i(TAG, "change2BaseEntity: "+ob);
        return ob;
    }


    @Override
    public Observable<BaseEntity> request_refresh_load(Map<String, Object> map) {
        return change2BaseEntity(map);
    }
}
